<!DOCTYPE html><html lang="zh-CN"><head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Typescript | Moment.js &#x6587;&#x6863;</title>
  <link rel="stylesheet" href="static/css/style.css">
  <link rel="shortcut icon" href="http://momentjs.cn/static/img/moment-favicon.png">
  <link rel="stylesheet" href="static/css/atom-one-light.min.css">
</head>
<body class="is-moment">
  <div class="hero hero-oneline">
    <div class="hero-centered">
      <h1>Typescript</h1>
    </div>
  </div>
  <div id="api-section-__FILENAME__">
    <div id="content" class="clearfix">
  
      <div id="column1" data-id="__ID__" class="interior">
        <header>
          <!-- <h1>Typescript</h1> -->
          <div id="gtoc">
            <ul>
              <!-- <li>v</li> -->
              <li>
                <a href="index.html#/use-it/typescript/" name="toc">&#x8FD4;&#x56DE;&#x4E0A;&#x5C42;&#x6587;&#x6863;</a>
              </li>
              <li style="border: none;">
                <a href="javascript:window.open('https://github.com/momentjscn/moment-docs-cn/edit/master/moment/00-use-it/09-typescript.md');" rel="nofollow" target="_blank">&#x63D0;&#x4EA4;&#x4FEE;&#x6539;</a>
              </li>
              <!-- <li>
                <a href="/search__SEARCH_PARAM__" name="toc">搜索</a>
              </li> -->
            </ul>
          </div>
          <hr>
        </header>

        <div id="biz_item"></div>
  
        <div id="apicontent">
          <div id="content_left">
<p>&#x4ECE; <strong>2.13.0</strong> &#x7248;&#x672C;&#x5F00;&#x59CB;&#xFF0C;Moment &#x4F1A;&#x5305;&#x542B;&#x4E00;&#x4E2A; typescript &#x5B9A;&#x4E49;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x901A;&#x8FC7;NPM&#x5B89;&#x88C5;&#x3002;</p>
<pre><code>npm install moment
</code></pre>
<p>&#x5BFC;&#x5165;&#x5E76;&#x5728;&#x4F60;&#x7684; Typescript &#x6587;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528;&#x3002;</p>
<!-- skip-example -->
<pre><code class="language-javascript">import * as moment from &apos;moment&apos;;

let now = moment().format(&apos;LLLL&apos;);
</code></pre>
<p>&#x5982;&#x679C;&#x4F60;&#x5728;&#x5BFC;&#x5165;&#x65F6;&#x9047;&#x5230;&#x9EBB;&#x70E6;&#x3002;</p>
<p>&#x5BF9;&#x4E8E; Typescript 2.x&#xFF0C;&#x5219;&#x5C1D;&#x8BD5;&#x5728; <code>tsconfig.json</code> &#x6587;&#x4EF6;&#x4E2D;&#x7684; <code>compileOptions</code> &#x4E2D;&#x6DFB;&#x52A0; <code>&quot;moduleResolution&quot;: &quot;node&quot;</code>&#xFF0C;&#x7136;&#x540E;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x4EFB;&#x4F55;&#x8BED;&#x6CD5;&#xFF1A;</p>
<!-- skip-example -->
<pre><code class="language-javascript">import * as moment from &apos;moment&apos;;
import moment = require(&apos;moment&apos;);
</code></pre>
<p>&#x5BF9;&#x4E8E; Typescript 1.x&#xFF0C;&#x5219;&#x5C1D;&#x8BD5;&#x5728; <code>tsconfig.json</code> &#x6587;&#x4EF6;&#x4E2D;&#x7684; <code>compileOptions</code> &#x4E2D;&#x6DFB;&#x52A0; <code>&quot;allowSyntheticDefaultImports&quot;: true</code>&#xFF0C;&#x7136;&#x540E;&#x4F7F;&#x7528;&#x8BED;&#x6CD5;&#xFF1A;</p>
<!-- skip-example -->
<pre><code class="language-javascript">import moment from &apos;moment&apos;;
</code></pre>
<p><strong>&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x5BFC;&#x5165;</strong></p>
<p>&#x82E5;&#x8981;&#x4F7F;&#x7528; <code>moment.locale</code>&#xFF0C;&#x5219;&#x9996;&#x5148;&#x9700;&#x8981;&#x5BFC;&#x5165;&#x8981;&#x4F7F;&#x7528;&#x7684;&#x8BED;&#x8A00;&#x3002;</p>
<!-- skip-example -->
<pre><code class="language-javascript">import * as moment from &apos;moment&apos;;
import &apos;moment/locale/pt-br&apos;;

console.log(moment.locale()); // en
moment.locale(&apos;fr&apos;);
console.log(moment.locale()); // en
moment.locale(&apos;pt-BR&apos;);
console.log(moment.locale()); // pt-BR
</code></pre>
					</div>
          <div id="content_right">
<p>As of version <strong>2.13.0</strong>, Moment includes a typescript definition file.</p>
<p>Install via NPM</p>
<pre><code>npm install moment
</code></pre>
<p>Import and use in your Typescript file</p>
<!-- skip-example -->
<pre><code class="language-javascript">import * as moment from &apos;moment&apos;;

let now = moment().format(&apos;LLLL&apos;);
</code></pre>
<p><strong>Note:</strong> If you have trouble importing moment</p>
<p>For <em>Typescript 2.x</em> try adding <code>&quot;moduleResolution&quot;: &quot;node&quot;</code> in <code>compilerOptions</code> in your <code>tsconfig.json</code> file and then use any of the below syntax</p>
<!-- skip-example -->
<pre><code class="language-javascript">import * as moment from &apos;moment&apos;;
import moment = require(&apos;moment&apos;);
</code></pre>
<p>For <em>Typescript 1.x</em> try adding <code>&quot;allowSyntheticDefaultImports&quot;: true</code> in <code>compilerOptions</code> in your <code>tsconfig.json</code> file and then use the syntax</p>
<!-- skip-example -->
<pre><code class="language-javascript">import moment from &apos;moment&apos;;
</code></pre>
<p><strong>Locale Import</strong></p>
<p>To use <code>moment.locale</code> you first need to import the language you are targeting.</p>
<!-- skip-example -->
<pre><code class="language-javascript">import * as moment from &apos;moment&apos;;
import &apos;moment/locale/pt-br&apos;;

console.log(moment.locale()); // en
moment.locale(&apos;fr&apos;);
console.log(moment.locale()); // en
moment.locale(&apos;pt-BR&apos;);
console.log(moment.locale()); // pt-BR
</code></pre>
					</div>
          <div></div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="static/js/highlight.min.js"></script>
  <script src="static/js/javascript.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
  <script>var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?7a7f78c805fbacc7e075d34f153031aa"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();</script>

  <style>
    .hero-centered {
      margin: 0;
          margin-left: 10px;
    }
#gtoc ul {
  list-style: none;
  margin-left: 0;
  line-height: 1.5rem;
  padding: 0;
}
#gtoc>ul>li {
  display: inline;
  border-right: 1px solid #000;
  margin-right: .4rem;
  padding-right: .4rem;
}
#gtoc>ul>li>a {
  color: #61b2a7;
}
#content_left {
  width: 49%;float: left;border-right: 1px solid #eee;    padding-right: 2%;
}
#content_right {
float: left;width: 48%;margin-left: 3%;
}
#column1 {
  padding: 0 2rem;
}
  </style>


</body></html>